package com.qh.xx.栈;

import java.util.Stack;

/**
 * @description:
 * @author: zqh
 * @date: 2021/11/5 12:21
 * https://leetcode-cn.com/problems/implement-queue-using-stacks/
 *
 * 思想：
 * 准备两个队列  inStack 、outStack 一个入队  一个出队  互相配合
 *
 **/
public class _232_用栈实现队列 {
    private Stack<Integer> inStack =  new Stack<>();
    private Stack<Integer> outStack =  new Stack<>();

    class MyQueue {

        public MyQueue() {

        }

        /** 入队 */
        public void push(int x) {
            inStack.push(x);
        }

        /** 出队 */
        public int pop() {
            if (outStack.isEmpty()){
                while (inStack.isEmpty()){
                    outStack.push(inStack.pop());
                }
            }
            return outStack.pop();
        }

        /** 获取队头元素 */
        public int peek() {
            if (outStack.isEmpty()){
                while (inStack.isEmpty()){
                    outStack.push(inStack.pop());
                }
            }
            return outStack.peek();
        }

        /** 是否为空 */
        public boolean empty() {
            return inStack.isEmpty() && outStack.isEmpty();
        }
    }

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */
}
